package com.huakang.service.impl;

import com.huakang.entity.*;
import com.huakang.mapper.AssetMapper;
import com.huakang.mapper.MedicionMapper;
import com.huakang.mapper.PrescriptionMapper;
import com.huakang.mapper.SeeadoctorMapper;
import com.huakang.service.PrescriptionService;
import com.huakang.utils.PrescriptionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author{一只小呆瓜}
 * @Date2025/1/11 20:15
 */
@Service
@Transactional
public class PrescriptionServiceImpl implements PrescriptionService {
    @Autowired
    private PrescriptionMapper prescriptionMapper;

    @Autowired
    private MedicionMapper medicionMapper;

    @Autowired
    private AssetMapper assetMapper;

    @Autowired
    private SeeadoctorMapper seeadoctorMapper;

    @Override
    public List<PrescriptionVo> findname() {
        return prescriptionMapper.findname();
    }

    @Override
    public boolean add(PrescriptionVo prescription) {
        // 修改asset表的药品总价
        Integer prid = prescription.getPrid();
        Prescription key = prescriptionMapper.selectByPrimaryKey(prid);
        Integer sid = key.getSid();
        Seeadoctor seeadoctor = seeadoctorMapper.selectByPrimaryKey(sid);
        Integer pid = seeadoctor.getPid();
        AssetExample assetExample = new AssetExample();
        assetExample.createCriteria().andPidEqualTo(pid);
        List<Asset> assets = assetMapper.selectByExample(assetExample);
        if (assets.size() != 0){
            Integer aid = assets.get(0).getAid();
            Asset asset = new Asset();
            asset.setAid(aid);
            asset.setMedic(prescription.getMedic());
            assetMapper.updateByPrimaryKeySelective(asset);
        }

        MedicionExample example = new MedicionExample();
        example.createCriteria().andMdnameEqualTo(prescription.getMdname());
        List<Medicion> medicions = medicionMapper.selectByExample(example);
        prescription.setMedid(medicions.get(0).getMedid());
        return prescriptionMapper.updateByPrimaryKeySelective(prescription)>0?true:false;
    }

    @Override
    public List<PrescriptionVo> findPid(Integer pid) {
        return prescriptionMapper.findPid(pid);
    }

    @Override
    public boolean update(Prescription prescription) {
        return prescriptionMapper.updateByPrimaryKeySelective(prescription)>0?true:false;
    }

    @Override
    public boolean addchufang(Prescription prescription) {

        return prescriptionMapper.insertSelective(prescription)>0?true:false;
    }


}
